home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / client.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  10KB  |  298 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: client.py 678 2008-08-08 11:22:14Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import threading
  7. import logging
  8. from pyxmpp.clientstream import ClientStream
  9. from pyxmpp.iq import Iq
  10. from pyxmpp.presence import Presence
  11. from pyxmpp.roster import Roster
  12. from pyxmpp.exceptions import ClientError, FatalClientError
  13. from pyxmpp.interfaces import IPresenceHandlersProvider, IMessageHandlersProvider
  14. from pyxmpp.interfaces import IIqHandlersProvider, IStanzaHandlersProvider
  15.  
  16. class Client:
  17.     
  18.     def __init__(self, jid = None, password = None, server = None, port = 5222, auth_methods = ('sasl:DIGEST-MD5',), tls_settings = None, keepalive = 0):
  19.         self.jid = jid
  20.         self.password = password
  21.         self.server = server
  22.         self.port = port
  23.         self.auth_methods = list(auth_methods)
  24.         self.tls_settings = tls_settings
  25.         self.keepalive = keepalive
  26.         self.stream = None
  27.         self.lock = threading.RLock()
  28.         self.state_changed = threading.Condition(self.lock)
  29.         self.session_established = False
  30.         self.roster = None
  31.         self.stream_class = ClientStream
  32.         if not hasattr(self, 'interface_providers'):
  33.             self.interface_providers = [
  34.                 self]
  35.         
  36.         self._Client__logger = logging.getLogger('pyxmpp.Client')
  37.  
  38.     
  39.     def connect(self, register = False):
  40.         if not self.jid:
  41.             raise ClientError, 'Cannot connect: no or bad JID given'
  42.         
  43.         self.lock.acquire()
  44.         
  45.         try:
  46.             stream = self.stream
  47.             self.stream = None
  48.             if stream:
  49.                 import common as common
  50.                 common.netcall(stream.close)
  51.             
  52.             self._Client__logger.debug('Creating client stream: %r, auth_methods=%r' % (self.stream_class, self.auth_methods))
  53.             stream = self.stream_class(jid = self.jid, password = self.password, server = self.server, port = self.port, auth_methods = self.auth_methods, tls_settings = self.tls_settings, keepalive = self.keepalive, owner = self)
  54.             stream.process_stream_error = self.stream_error
  55.             self.stream_created(stream)
  56.             stream.state_change = self._Client__stream_state_change
  57.             stream.connect()
  58.             self.stream = stream
  59.             self.state_changed.notify()
  60.             self.state_changed.release()
  61.         except:
  62.             self.stream = None
  63.             self.state_changed.release()
  64.             raise 
  65.  
  66.  
  67.     
  68.     def get_stream(self):
  69.         self.lock.acquire()
  70.         stream = self.stream
  71.         self.lock.release()
  72.         return stream
  73.  
  74.     
  75.     def disconnect(self):
  76.         stream = self.get_stream()
  77.         if stream:
  78.             stream.disconnect()
  79.         
  80.  
  81.     
  82.     def request_session(self):
  83.         stream = self.get_stream()
  84.         if not stream.version:
  85.             need_session = False
  86.         elif not stream.features:
  87.             need_session = False
  88.         else:
  89.             ctxt = stream.doc_in.xpathNewContext()
  90.             ctxt.setContextNode(stream.features)
  91.             ctxt.xpathRegisterNs('sess', 'urn:ietf:params:xml:ns:xmpp-session')
  92.             ctxt.xpathRegisterNs('jsess', 'http://jabberd.jabberstudio.org/ns/session/1.0')
  93.             sess_n = None
  94.             
  95.             try:
  96.                 sess_n = ctxt.xpathEval('sess:session or jsess:session')
  97.             finally:
  98.                 ctxt.xpathFreeContext()
  99.  
  100.             if sess_n:
  101.                 need_session = True
  102.             else:
  103.                 need_session = False
  104.         if not need_session:
  105.             self.state_changed.acquire()
  106.             self.session_established = 1
  107.             self.state_changed.notify()
  108.             self.state_changed.release()
  109.             self._session_started()
  110.         else:
  111.             iq = Iq(stanza_type = 'set')
  112.             iq.new_query('urn:ietf:params:xml:ns:xmpp-session', 'session')
  113.             stream.set_response_handlers(iq, self._Client__session_result, self._Client__session_error, self._Client__session_timeout)
  114.             stream.send(iq)
  115.  
  116.     
  117.     def request_roster(self):
  118.         stream = self.get_stream()
  119.         iq = Iq(stanza_type = 'get')
  120.         iq.new_query('jabber:iq:roster')
  121.         stream.set_response_handlers(iq, self._Client__roster_result, self._Client__roster_error, self._Client__roster_timeout)
  122.         stream.set_iq_set_handler('query', 'jabber:iq:roster', self._Client__roster_push)
  123.         stream.send(iq)
  124.  
  125.     
  126.     def get_socket(self):
  127.         return self.stream.socket
  128.  
  129.     
  130.     def loop(self, timeout = 1):
  131.         while None:
  132.             stream = self.get_stream()
  133.             if not stream:
  134.                 break
  135.             
  136.             act = stream.loop_iter(timeout)
  137.             if not act:
  138.                 self.idle()
  139.                 continue
  140.             continue
  141.             return None
  142.  
  143.     
  144.     def __session_timeout(self):
  145.         raise FatalClientError('Timeout while tryin to establish a session')
  146.  
  147.     
  148.     def __session_error(self, iq):
  149.         err = iq.get_error()
  150.         msg = err.get_message()
  151.         raise FatalClientError('Failed to establish a session: ' + msg)
  152.  
  153.     
  154.     def __session_result(self, _unused):
  155.         self.state_changed.acquire()
  156.         self.session_established = True
  157.         self.state_changed.notify()
  158.         self.state_changed.release()
  159.         self._session_started()
  160.  
  161.     
  162.     def _session_started(self):
  163.         for ob in self.interface_providers:
  164.             if IPresenceHandlersProvider.providedBy(ob):
  165.                 for handler_data in ob.get_presence_handlers():
  166.                     self.stream.set_presence_handler(*handler_data)
  167.                 
  168.             
  169.             if IMessageHandlersProvider.providedBy(ob):
  170.                 for handler_data in ob.get_message_handlers():
  171.                     self.stream.set_message_handler(*handler_data)
  172.                 
  173.             
  174.             if IIqHandlersProvider.providedBy(ob):
  175.                 for handler_data in ob.get_iq_get_handlers():
  176.                     self.stream.set_iq_get_handler(*handler_data)
  177.                 
  178.                 for handler_data in ob.get_iq_set_handlers():
  179.                     self.stream.set_iq_set_handler(*handler_data)
  180.                 
  181.         
  182.         self.session_started()
  183.  
  184.     
  185.     def __roster_timeout(self):
  186.         raise ClientError('Timeout while tryin to retrieve roster')
  187.  
  188.     
  189.     def __roster_error(self, iq):
  190.         err = iq.get_error()
  191.         msg = err.get_message()
  192.         raise ClientError('Roster retrieval failed: ' + msg)
  193.  
  194.     
  195.     def __roster_result(self, iq):
  196.         q = iq.get_query()
  197.         if q:
  198.             self.state_changed.acquire()
  199.             self.roster = Roster(q)
  200.             self.state_changed.notify()
  201.             self.state_changed.release()
  202.             self.roster_updated()
  203.         else:
  204.             raise ClientError('Roster retrieval failed')
  205.  
  206.     
  207.     def __roster_push(self, iq):
  208.         fr = iq.get_from()
  209.         if fr and fr.bare() != self.jid.bare():
  210.             resp = iq.make_error_response('forbidden')
  211.             self.stream.send(resp)
  212.             raise ClientError('Got roster update from wrong source')
  213.         
  214.         if not self.roster:
  215.             raise ClientError('Roster update, but no roster')
  216.         
  217.         q = iq.get_query()
  218.         items = self.roster.update(q)
  219.         for item in items:
  220.             self.roster_updated(item)
  221.         
  222.         resp = iq.make_result_response()
  223.         self.stream.send(resp)
  224.  
  225.     
  226.     def __stream_state_change(self, state, arg):
  227.         self.stream_state_changed(state, arg)
  228.         if state == 'fully connected':
  229.             self.connected()
  230.         elif state == 'authorized':
  231.             self.authorized()
  232.         elif state == 'disconnected':
  233.             self.state_changed.acquire()
  234.             
  235.             try:
  236.                 if self.stream:
  237.                     self.stream.close()
  238.                 
  239.                 self.stream_closed(self.stream)
  240.                 self.stream = None
  241.                 self.state_changed.notify()
  242.             finally:
  243.                 self.state_changed.release()
  244.  
  245.             self.disconnected()
  246.         
  247.  
  248.     
  249.     def idle(self):
  250.         stream = self.get_stream()
  251.         if stream:
  252.             stream.idle()
  253.         
  254.  
  255.     
  256.     def stream_created(self, stream):
  257.         pass
  258.  
  259.     
  260.     def stream_closed(self, stream):
  261.         pass
  262.  
  263.     
  264.     def session_started(self):
  265.         self.request_roster()
  266.         p = Presence()
  267.         self.stream.send(p)
  268.  
  269.     
  270.     def stream_error(self, err):
  271.         self._Client__logger.error('Stream error: condition: %s %r' % (err.get_condition().name, err.serialize()))
  272.  
  273.     
  274.     def roster_updated(self, item = None):
  275.         pass
  276.  
  277.     
  278.     def stream_state_changed(self, state, arg):
  279.         pass
  280.  
  281.     
  282.     def connected(self):
  283.         pass
  284.  
  285.     
  286.     def authenticated(self):
  287.         pass
  288.  
  289.     
  290.     def authorized(self):
  291.         self.request_session()
  292.  
  293.     
  294.     def disconnected(self):
  295.         pass
  296.  
  297.  
  298.